package controle.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entidades.RankngResumo;
import banco.maneger.RankngManeger;

import com.google.gson.Gson;

public class VerificarAction implements ActionCommand  {

	@Override
	public void executeAction(HttpServletRequest request, HttpServletResponse response) {
		RankngManeger maneger = new RankngManeger();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        String json = "";
        if(br != null){
            try {
				json = br.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            Gson gson = new Gson();
            RankngResumo rankngResumo = gson.fromJson(json, RankngResumo.class);
            RankngResumo rankngRetorno = maneger.pesquisaPorNome(rankngResumo);
            if(rankngRetorno.getNome() != null){
            	maneger.alteraForNome(rankngResumo);
            }else{
            	maneger.adiciona(rankngResumo);
            }
            
        }
        maneger.closeConnection();
	}
}
